﻿@{
    ViewBag.MainMenu = "System";
    ViewBag.LeftMenu = "Password";

    ViewBag.Title = "修改密码";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@Scripts.Render("~/bundles/md5")

<style type="text/css">
    .divBaseSettingsTitle {
        margin-top: 25px;
        margin-left: 10px;
        margin-right: 10px;
    }

    .divBaseSettings {
        margin-top: 20px;
        margin-left: 10px;
        margin-right: 10px;
    }
</style>

<script language="javascript">


    var _validator;

    $(document).ready(function () {

        $("[keyenter]").keypress(function (e) {
            if (e.keyCode == 13) {
                save();
            }
        });

        _validator = $("#form").validate({
            onfocusout: false,
            onkeyup: false,
            showErrors: showValidationErrors,
            rules: {
                "txtPassword": "required",
                "txtNewPassword": "required",
                "txtNewPassword2": {
                    required: true,
                    minlength: 3,
                    equalTo: "#txtNewPassword"
                }
            },
            messages: {
                "txtPassword": "请输入当前密码；",
                "txtNewPassword": "请输入新密码；",
                "txtNewPassword2": {
                    required: "请再次输入新密码；",
                    minlength: "密码长度至少在3位以上；",
                    equalTo: "两次新密码输入不一致；"
                }
            }
        });
    });

    function save() {
        if (_validator.form() == false) {
            return;
        }

        var args = new Object();
        args.Password = hex_md5($("#txtPassword").val()).toUpperCase();
        args.NewPassword = hex_md5($("#txtNewPassword").val()).toUpperCase();

        var layerIndex = layer.load(0, {
            shade: [0.2, '#fff']
        });

        $.ajax({
            url: "/Api/User/UpdatePassword",
            type: "POST",
            dataType: "json",
            data: JSON.stringify(args),
            success: function (data, status, jqXHR) {
                if (data.Success) {
                    layer.close(layerIndex);

                    //layer.msg("密码已更新。", {
                    //    time: 1000
                    //});

                    layerAlert("密码已更新，下次请使用新密码登录。");

                    $("#txtPassword").val("");
                    $("#txtNewPassword").val("");
                    $("#txtNewPassword2").val("");

                } else {
                    layer.closeAll();

                    layerAlert("密码更新失败，请确认当前密码输入是否正确。");
                }
            },
            error: function (xmlHttpRequest) {
                alert("Error: " + xmlHttpRequest.status);
            }
        });
    }
</script>


<div id="divContentTips">
    修改您的登录密码。
</div>

<div class="divBaseSettings">
    <form id="form">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td width="200" height="36" valign="top">当前密码：</td>
                <td valign="top"><input name="txtPassword" type="password" class="input_16" style="width:400px;" id="txtPassword" maxlength="50" keyenter /></td>
            </tr>
            <tr>
                <td height="36" valign="top">新密码：</td>
                <td valign="top"><input name="txtNewPassword" type="password" class="input_16" style="width:400px;" id="txtNewPassword" maxlength="50" keyenter /></td>
            </tr>
            <tr>
                <td height="36" valign="top">再次输入新密码：</td>
                <td valign="top"><input name="txtNewPassword2" type="password" class="input_16" style="width:400px;" id="txtNewPassword2" maxlength="50" keyenter /></td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <div class="font_gray_13" style="width:96%;margin-bottom:10px;">
                        密码至少3位以上。
                    </div>
                </td>
            </tr>
        </table>
    </form>
</div>

<div style=" margin-top:15px;">
    <input name="btnSave" type="button" class="btn_blue" id="btnStep2Next" value="保存" onclick="save()" />
</div>

